package com.example.addlist;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.content.*;

import java.util.ArrayList;  

public class Modification extends Activity{
//
	private Button modify;
	private Button call;
	private EditText nameedit;
	private EditText phoneedit;
	private EditText addressedit;
	private int totalnum;
	@Override	
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.modification1_layout);
		nameedit=(EditText) findViewById(R.id.nameedit);
		phoneedit=(EditText) findViewById(R.id.phoneedit);
		addressedit=(EditText) findViewById(R.id.addressedit);
		call=(Button) findViewById(R.id.Call);
        SharedPreferences pref=getSharedPreferences("data1",MODE_PRIVATE);    

   
        Intent intent=getIntent();	
        String compare=new String("add");

        
        String thereturn=intent.getStringExtra("comm");
		if(thereturn.equals(compare))
		{

			
		}
		else
		{			
		//nameedit.setText(thereturn);
		nameedit.setText(pref.getString("name"+thereturn, ""));
		phoneedit.setText(pref.getString("phone"+thereturn, ""));
		addressedit.setText(pref.getString("address"+thereturn, ""));				
		}			
		modify=(Button) findViewById(R.id.modify);
		
		
		
		modify.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					
			        SharedPreferences pref=getSharedPreferences("data1",MODE_PRIVATE);   

			        Intent intent=getIntent();		
			        
			        if(intent.getStringExtra("comm").equals("add"))
			        		{
				        totalnum=pref.getInt("totlnum",0);
				        totalnum++;
						SharedPreferences.Editor editor=getSharedPreferences("data1",MODE_PRIVATE).edit();
						editor.putString("name"+ Integer.toString(totalnum), nameedit.getText().toString());
						editor.putString("phone"+ Integer.toString(totalnum), phoneedit.getText().toString());
						editor.putString("address"+ Integer.toString(totalnum), addressedit.getText().toString());
						editor.putInt("totlnum", totalnum);
						editor.commit();	
						
			        		}
			        
			        
			        else
			        {
			        
			       	       
					SharedPreferences.Editor editor=getSharedPreferences("data1",MODE_PRIVATE).edit();
					editor.putString("name"+ intent.getStringExtra("comm"), nameedit.getText().toString());
					editor.putString("phone"+ intent.getStringExtra("comm"), phoneedit.getText().toString());
					editor.putString("address"+ intent.getStringExtra("comm"), addressedit.getText().toString());
					editor.commit();	
			        
			        }
	

					intent.putExtra("data_return", nameedit.getText().toString());
					setResult(RESULT_OK,intent);
					finish();
				}
			});
		

		call.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(Intent.ACTION_DIAL);
				intent.setData(Uri.parse("tel:"+phoneedit.getText().toString()));
				startActivity(intent);
			}
		});
		
		
	}
}



























